$(function() {
	var $cards = $(".main li"), //所有卡牌
		$maskCard = $("#mask-card"), //翻牌遮罩
		length = $cards.length,
		index = length, //轮流滚动的卡牌下标
		hb_data = {
			count: 0
		}, //次数
		width = $(".main li").width(),
		initArr = [
			[width * 2 + 42.5, 15],
			[width + 31.5, 15],
			[19.5, 15],
			[width * 2 + 42.5, 150],
			[width + 31.5, 150],
			[19.5, 150]
		], //卡牌位置数组
		clickTime = 0,
		bool = false, //首次点击时不能在卡牌归位期间
		timer; //轮流提示定时器
	var hb_code = '14'
	var yxbh = sessionStorage.getItem("hb_yxbh")
	var flag = 1
	/*alert(yxbh)*/
	if(yxbh == null || yxbh == undefined) {
		yxbh = ''
	}
	YDUI.dialog.loading.open('很快加载好了')
	//获取正在进行的活动
	$.post("selectActivityDateType", {
		activityType: '14'
	}, function(data) {
		/*mui.alert(JSON.stringify(data))*/
		if(data.code == 0) {
			var code = data.data.活动代码
			$("title").text(data.data.活动名称)
			abb(code)
			$.post("updateActivityUserShare", {
				activityType: code,
				number4: yxbh
			}, function(data) {
				/*alert(JSON.stringify(data))*/
				if(data.code == 0) {

				}
			})
		} else {
			YDUI.dialog.alert("活动已结束，敬请期待下次活动开启", function() {
				window.location.replace("wx_hblb")
			})
		}
	})

	function abb(code) {
		$.post("userIfDrawHhsw", {
			activityType: code,
			status: 0,
		}, function(data) {
			if(data.code == 0) {
				hb_data.count = data.data
				$(".cjjh b").text(hb_data.count)
				YDUI.dialog.loading.close()
				init();
			} else if(data.message == "用户登录超时，请重新登录") {
				window.location.replace("http://yykh.czzrcb.com/zzhddj/getHhswCode")

			} else {
				hb_data.count = 0
				YDUI.dialog.loading.close()
				init();
			}

		})
	}

	function init() {
		//卡牌归位动画
		for(var i = 0; i < length; i++) {
			(function(i) {
				var seat = setTimeout(function() {
					$cards.eq(i).css({
						right: initArr[i][0] + "px",
						top: initArr[i][1] + "px"
					});
					clearTimeout(seat);
				}, 300 * i);
			})(i);
		}

		//卡牌轮流选中动画
		setTimeout(function() {
			timer = setInterval(function() {
				bool = true; //卡牌归位，可以点击
				$cards.eq(index - 1).removeClass("active");
				index %= 6;
				$cards.eq(index).addClass("active");
				index++;
			}, 1000);
		}, 1000);
	}

	//点击卡牌翻转
	$(".main").on("click", "li", function() {
		if(flag == 1 && bool) { //两次点击的间隔不能小于2秒
			if(hb_data.count > 0) {
				flag = 0
				var index=$(this).index();
                  var tt=document.styleSheets[0];
                 if(index==0){
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(360deg);} }",9);//写入样式
                 }else if(index==1){
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(660deg);} }",9);
                 }else if(index==2){
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(600deg);} }",9);
                 }else if(index==3){
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(540deg);} }",9);
                 }else if(index==4){
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(480deg);} }",9);
                 }else{
                 	tt.insertRule("@keyframes fn2 { 0% {transform:rotateX(-15deg) rotateY(0deg);} 100% {transform:rotateX(0deg) rotateY(420deg);} }",9);
                 }
				$maskCard.show();
				clearInterval(timer); //清除轮流选中动画
				$cards.removeClass("active"); //清除轮流滚动类名
				 $maskCard.find(".box").addClass("boxdh")
				//动画监听
				$(".box").on("animationend", function() {
					var row = $(this)
					YDUI.dialog.loading.open('很快加载好了')
					$.post("luckDrawHhsw", {
						activityType: '14',
						number4: yxbh
					}, function(data) {
						// alert(JSON.stringify(data))
						if(data.code == 0) {
							var jpid = data.data;
							hb_data.count = Number(hb_data.count) - 1
							$(".cjjh b").text(hb_data.count)
							if(jpid == '56') {
								$("#zjtp").attr("src", "img/601.png")
							} else if(jpid == '57') {
								$("#zjtp").attr("src", "img/801.png")
							} else if(jpid == "58") {
								$("#zjtp").attr("src", "img/101.png")
							} else if(jpid == "59") {
								$("#zjtp").attr("src", "img/121.png")
							} else if(jpid == "60") {
								$("#zjtp").attr("src", "img/991.png")
							}
							$cards.css({ //所有卡牌放到右下角
								right: 15 + "px",
								top: 150 + "px"
							});
							$maskCard.hide();
							win();
							YDUI.dialog.loading.close()
							row.off("animationend"); //解绑动画监听
						} else {
							YDUI.dialog.alert(data.message);
							YDUI.dialog.loading.close()
						}
					})

				});
			} else {
				YDUI.dialog.alert("没有抽奖次数了~");
			}
		}
	});

	//中奖信息提示
	$("#close,.win,.btn").click(function() {
		clickTime = new Date(); //时间更新
		index = length; //卡牌选中重新从第一张开始
		init();
	});
	//领奖跳转
	$("#zjtp").click(function() {
		$.post("selectListByOpenId", {
			activityType: '14'
		}, function(data) {
			/*alert(JSON.stringify(data))*/
			if(data.code == 0 && data.data.length > 0) {
				var rel = data.data[0];
				if(rel.姓名 == undefined || rel.姓名 == "") {
					window.location.replace("wx_hbxx")
				} else {
					window.location.replace("wx_hblb")
				}
			} else {
				window.location.replace("wx_hbxx")
			}
		})
	})
	$(".btn1").click(function() { //点击中奖记录
		$.post("selectListByOpenId", function(data) {
			/*alert(JSON.stringify(data))*/
			if(data.code == 0 && data.data.length > 0) {
				var rel = data.data[0];
				if(rel.姓名 == undefined || rel.姓名 == "") {
					window.location.replace("wx_hbxx")
				} else {
					window.location.replace("wx_hblb")
				}
			} else {
				window.location.replace("wx_hbxx")
			}
		})

	})
	$(".btn2").click(function() { //点击邀请好友
		$.post("selectListByOpenId", function(data) {
			/*alert(JSON.stringify(data))*/
			if(data.code == 0 && data.data.length > 0) {
				var rel = data.data[0];
				if(rel.姓名 == undefined || rel.姓名 == "") {
					window.location.replace("wx_hbxx")
				} else {
					window.location.replace("wx_hbfx")
				}
			} else {
				window.location.replace("wx_hbxx")
			}
		})
	})

	function win() {
		//遮罩层显示
		$("#mask").show();
		$(".winning").addClass("reback");
		setTimeout(function() {
			$("#card").addClass("pull");
		}, 500);
		//关闭弹出层
		$("#close,.win,.btn").click(function() {
			//$close.click(function () {
			$("#mask").hide();
			$(".box").removeClass("boxdh")
			var tt=document.styleSheets[0];
        	tt.deleteRule(9)
			$(".winning").removeClass("reback");
			flag = 1
		});
	}
});